import { arriveTo, goTo } from '@/modules/move'

export const claimerFn = (creep: Creep) => {
    if (creep.memory.boost && !creep.memory.boost.done) {
        return
    }

    const {
        targetRoom,
        targetShard,
        sign = 'I love you, will you be my girlfriend?',
        type = 'claim',
    } = creep.memory.data as {
        targetRoom: string
        targetShard: string
        sign: string
        type: 'attack' | 'reserve' | 'claim'
    }

    if (creep.hits < creep.hitsMax && creep.getActiveBodyparts(HEAL) > 0) {
        creep.heal(creep)
    }

    if (!targetRoom || !targetShard) return
    if (creep.room.name !== targetRoom || Game.shard.name !== targetShard) {
        arriveTo(creep, new RoomPosition(25, 25, targetRoom), targetShard, 10)
        return
    }

    const controller = creep.room.controller
    if (!controller) {
        return
    }

    if (!creep.pos.isNearTo(controller)) {
        goTo(creep, controller.pos)
        return
    }

    if (controller.owner && controller.owner.username !== creep.owner.username) {
        creep.attackController(controller)
    } else if (!controller.reservation) {
        if (type === 'reserve') {
            creep.reserveController(controller)
        } else if (type === 'claim') {
            const result = creep.claimController(controller)
            if (result === OK) {
                creep.suicide()
            }
        }
    }
    // 别人在 reserve
    else if (controller.reservation.username !== creep.owner.username) {
        creep.attackController(controller)
    }

    if (controller.sign?.text !== sign) {
        creep.signController(controller, sign)
    }
}
